package hanlder

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"repo-master/cache"
	"repo-master/models"
	"repo-master/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	var user models.User
	redis_key := "Login_" + username
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if cacheData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "错误超过三次，禁止登录",
		})
		return
	}
	err := models.DB.Where("username = ?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if cacheData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "封禁两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
		"data": user,
	})
	return
}
func RoleOrderAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	oid, _ := strconv.Atoi(c.PostForm("oid"))
	data := models.RoleOrder{
		Rid: rid,
		Oid: oid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "关联成功",
	})
	return
}
func UserRoleList(c *gin.Context) {
	uid, _ := strconv.Atoi(c.Query("uid"))
	var user models.Role
	models.DB.
		Where("roles.uid = ?", uid).
		Find(&user)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
func RoleOrderList(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	var user models.RoleOrder
	models.DB.
		Where("rid = ?", rid).
		Find(&user)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
func KuAdd(c *gin.Context) {
	ku_name := c.PostForm("ku_name")
	ku_title := c.PostForm("ku_title")
	ku_address := c.PostForm("ku_address")
	data := models.Ku{
		KuName:    ku_name,
		KuTitle:   ku_title,
		KuAddress: ku_address,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func DingAdd(c *gin.Context) {
	ding_name := c.PostForm("ding_name")
	ding_types := c.PostForm("ding_types")
	ding_num := c.PostForm("ding_num")
	ding_price := c.PostForm("ding_price")
	kid, _ := strconv.Atoi(c.PostForm("kid"))
	data := models.Ding{
		DingName:  ding_name,
		DingTypes: ding_types,
		DingPrice: ding_price,
		DingNum:   ding_num,
		Kid:       kid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func KuDingList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var user []models.KuDingList
	redis_key := "ku_ding_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("dings").
			Select(
				"kus.id as KuId",
				"kus.ku_name as KuName",
				"kus.ku_title as KuTitle",
				"kus.ku_address as KuAddress",
				"dings.ding_name as DingName",
				"dings.ding_types as DingTypes",
				"dings.ding_num as DingNum",
				"dings.ding_price as DingPrice",
			).
			Joins("LEFT JOIN kus ON kus.id = dings.kid").
			Offset(offset).
			Limit(size).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}

	var total int64
	models.DB.Model(&models.Ding{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表展示成功",
		"data":  user,
		"total": total,
	})
	return
}
func KuDingList1(c *gin.Context) {
	kid := c.Query("kid")
	var user models.KuDingList
	redis_key := "ding_list" + kid
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("dings").
			Select(
				"kus.id as KuId",
				"kus.ku_name as KuName",
				"kus.ku_title as KuTitle",
				"kus.ku_address as KuAddress",
				"dings.ding_name as DingName",
				"dings.ding_types as DingTypes",
				"dings.ding_num as DingNum",
				"dings.ding_price as DingPrice",
			).
			Joins("LEFT JOIN kus ON kus.id = dings.kid").
			Where("dings.kid = ?", kid).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表展示成功",
		"data": user,
	})
	return
}
